import React, { Component } from 'react';
import { Form, Input, message} from 'antd';
import Model from '@/models/role'
const layout = {
    labelCol: {
      span: 8,
    },
    wrapperCol: {
      span: 16,
    },
  };
class AddRole extends Component {
    state = {
        alldata:[]
    }
    render() {
        return (
            <div>
                <Form
                    {...layout}
                    name="basic"
                    initialValues={{ remember: true }}
                    onFieldsChange={this.getallfields}
                >
                    <Form.Item
                        label="角色名"
                        name="roleName"
                        rules={[{ required: true, message: '请输入角色名！' }]}
                    >
                        <Input />
                    </Form.Item>
                </Form>
            </div>
        );
    }
    componentDidUpdate() {
        if (this.props.submitadd) {
            let { alldata } = this.state
            // console.log(this.state.alldata);
            let values = {}
            values['roleName'] = alldata[0] 
            // console.log(values);
            console.log('提交表单信息');
            Model.addRole(values).then(ret => {
                // console.log(ret);   
                if (ret.status === 0)
                    message.success("添加角色成功！")
                else
                    message.error("添加角色失败！")
            })
            return true
        }
        else
            return false
    }
    getallfields = (c, all) => {
        let alldata = []
        for (var i = 0; i < all.length; i++)
            alldata.push(all[i].value)
        this.setState({
            alldata
        })
    }
}

export default AddRole;
